package com.atguigu.vod.controller;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exception.GuliException;
import com.atguigu.vod.Utils.ConstantVodUtils;
import com.atguigu.vod.service.VodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitRetryTemplateCustomizer;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.Path;
import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/vod/video")
public class VodController {

    @Autowired
    private VodService vodService;


    @PostMapping("/uploadAlyiVideo")
    public R uploadAlyiVideo(MultipartFile file) throws IOException {

        String videoId = vodService.uploadVideoAly(file);

        return R.ok().data("videoId",videoId);
    }

    @DeleteMapping("/removeAlyiVideo/{id}")
    public R deleteAlyiVideo(@PathVariable String id){
        vodService.removeVideoAly(id);
        return R.ok();
    }

    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id)  {
        try{

            //1 根据视频Id 获取视频播放地址
            //创建初始化对象
            DefaultAcsClient defaultAcsClient =
                    ConstantVodUtils.initClient(ConstantVodUtils.ACCESS_REGION_ID,ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET);


            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();

            request.setVideoId(id);

            GetVideoPlayAuthResponse response = defaultAcsClient.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return R.ok().data("playAuth",playAuth);
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20001,"获取凭证失败");
        }

    }


    public static void main(String[] args) {
       int i =  19 % 20 ;
        System.out.println(i);
    }

}
